package com.wsoft.portal.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.core.exception.Asserts;
import com.wsoft.portal.entity.PortalOrgMemberEntity;
import com.wsoft.portal.entity.PortalThemeEntity;
import com.wsoft.portal.manager.PortalOrgMemberManager;
import com.wsoft.portal.manager.PortalThemeManager;
import com.wsoft.portal.mapper.PortalThemeMapper;
import com.wsoft.portal.query.theme.ThemeInsertQuery;
import com.wsoft.portal.query.theme.ThemeListQuery;
import com.wsoft.portal.query.theme.ThemeUpdateEnableQuery;
import com.wsoft.portal.query.theme.ThemeUpdateQuery;
import com.wsoft.portal.service.IPortalThemeService;
import com.wsoft.portal.utils.PortalUtil;
import com.wsoft.portal.vo.theme.ThemeVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 主题 服务实现类
 * </p>
 *
 * @author Z001
 * @since 2024年12月27日 10:00:40
 */
@Service
public class PortalThemeServiceImpl extends ServiceImpl<PortalThemeMapper, PortalThemeEntity> implements IPortalThemeService {

    @Resource
    private PortalThemeManager themeManager;
    @Resource
    private PortalOrgMemberManager orgMemberManager;
    @Resource
    private PortalUtil portalUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertTheme(ThemeInsertQuery query) {
        //校验主题code
        checkThemeCode(query.getCode(), null);
        PortalThemeEntity entity = Convert.convert(PortalThemeEntity.class, query);
        PortalThemeEntity defaultTheme = themeManager.getDefaultTheme();
        if (query.getIsDefault() == 1) {
            //清除之前默认主题
            themeManager.removeDefaultTheme();
        }
        boolean b = themeManager.save(entity);
        if (b) {
            //获取默认主题并复制
            PortalOrgMemberEntity themeDefault = orgMemberManager.getThemeDefault(defaultTheme.getId());
            if (null != themeDefault) {
                portalUtil.copyDefaultTheme(entity.getId(), themeDefault,null,null);
            }else{
                //默认主题不存在,无法复制
                Asserts.fail("[portal.default.theme.not.exist]");
            }
        }
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTheme(ThemeUpdateQuery query) {
        //校验主题code
        checkThemeCode(query.getCode(), query.getId());
        PortalThemeEntity entity = Convert.convert(PortalThemeEntity.class, query);
        if (query.getIsDefault() == 1) {
            //清除之前默认主题
            themeManager.removeDefaultTheme();
        }
        return themeManager.updateById(entity);
    }


    @Override
    public PortalThemeEntity getThemeId(Long id) {
        return themeManager.getById(id);
    }

    @Override
    public boolean deleteTheme(Long id) {
        return themeManager.removeById(id);
    }

    @Override
    public PageInfo<Object> listTheme(ThemeListQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        PageInfo pageInfo = PageInfo.of(themeManager.listTheme(query));
        List<PortalThemeEntity> list = pageInfo.getList();
        List<ThemeVO> themeVOList = new ArrayList<>();
        for (PortalThemeEntity entity : list) {
            ThemeVO themeVO = Convert.convert(ThemeVO.class, entity);
            PortalOrgMemberEntity themeDefault = orgMemberManager.getThemeDefault(entity.getId());
            if(null != themeDefault){
                themeVO.setPortalId(themeDefault.getId());
            }
            themeVOList.add(themeVO);
        }
        pageInfo.setList(themeVOList);
        return pageInfo;
    }

    @Override
    public boolean updateThemeEnable(ThemeUpdateEnableQuery query) {
        PortalThemeEntity themeEntity = themeManager.getById(query.getId());
        if(!themeEntity.getIsEnable().equals(query.getIsEnable())){
            themeEntity.setIsEnable(query.getIsEnable());
            themeManager.updateById(themeEntity);
        }
        return true;
    }

    // ################################## 私有方法  ##################################

    /**
     * 校验主题code
     *
     * @param moduleId
     * @param type
     * @param id
     */
    private void checkThemeCode(String code, Long id) {
        PortalThemeEntity entity = themeManager.getByCode(code);
        //新增
        if (null == id && entity != null) {
            Asserts.fail("[module.code.exist]");
        }
        //编辑
        if (null != id && entity != null && !id.equals(entity.getId())) {
            Asserts.fail("[module.code.exist]");
        }
    }

}
